﻿using Learun.Util;
using Learun.Util.SqlSugar;
using System;
using System.Text;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-01-30 15:57
    /// 描 述：图纸发布
    /// </summary>
    public class ec_drawing_publishService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_drawing_publishEntity> _drawingPublishRepository => new Repository<ec_drawing_publishEntity>();
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_drawing_publishEntity GetEntity(string keyValue, string ProjectId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    //return this.BaseRepository().FindEntity<ec_drawing_publishEntity>(keyValue);
                    return _drawingPublishRepository.GetById(keyValue);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    var strSql = new StringBuilder();
                    strSql.Append($@"SELECT * FROM ec_drawing_publish_{ ProjectEntity.ProjectIndex} WHERE DrawingPublishID = @DrawingPublishID ");
                    //List<ec_drawing_publishEntity> list = this.BaseRepository().FindList<ec_drawing_publishEntity>(strSql.ToString(), new { DrawingPublishID = keyValue }).ToList();
                    //return list.FirstOrDefault();
                    return SqlSugarHelper.Db.Ado.SqlQuerySingle<ec_drawing_publishEntity>(strSql.ToString(), new { DrawingPublishID = keyValue });
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue, string ProjectId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    //this.BaseRepository().Delete<ec_drawing_publishEntity>(t => t.DrawingPublishID == keyValue);
                    _drawingPublishRepository.Delete(t => t.DrawingPublishID == keyValue);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_drawing_publish_{ ProjectEntity.ProjectIndex} WHERE DrawingPublishID = @DrawingPublishID  ", new { DrawingPublishID = keyValue });
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, ec_drawing_publishEntity entity, string ProjectId)
        {
            try
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    entity.Modify(keyValue);
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        //this.BaseRepository().Update(entity);
                        _drawingPublishRepository.Update(entity);
                    }
                    else
                    {
                        var ProjectEntity = _projectRepository.GetById(ProjectId);
                        //this.BaseRepository().UpdateByNoMap(entity, $"ec_drawing_publish_{ ProjectEntity.ProjectIndex}", "DrawingPublishID");
                        _drawingPublishRepository.AsUpdateable(entity).AS($"ec_drawing_publish_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                }
                else
                {
                    entity.Create();
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _drawingPublishRepository.Insert(entity);
                    }
                    else
                    {
                        var ProjectEntity = _projectRepository.GetById(ProjectId);
                        //this.BaseRepository().InsertByNoMap(entity, $"ec_drawing_publish_{ ProjectEntity.ProjectIndex}");
                        _drawingPublishRepository.AsInsertable(entity).AS($"ec_drawing_publish_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
